package com.cn.pet.service.impl;

import com.cn.pet.data.Platform;
import com.cn.pet.mapper.ItemJdMapper;
import com.cn.pet.mapper.ItemPddMapper;
import com.cn.pet.mapper.ItemTbMapper;
import com.cn.pet.model.request.ItemQueryRequest;
import com.cn.pet.service.ItemService;
import com.cn.pet.util.DateUtil;
import com.cn.pet.vo.ItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    ItemTbMapper itemTbMapper;

    @Autowired
    ItemJdMapper itemJdMapper;

    @Autowired
    ItemPddMapper itemPddMapper;

    @Override
    public List<ItemVO> findItemList(ItemQueryRequest itemQueryRequest) {
        List<ItemVO> resultList = null;
        int platformId = itemQueryRequest.getPlatformId();
        if (Platform.TAO_BAO.getCode() == platformId) {
            resultList = itemTbMapper.queryByCondition(itemQueryRequest);
        } else if (Platform.JING_DONG.getCode() == platformId) {
            resultList = itemJdMapper.queryByCondition(itemQueryRequest);
        } else if (Platform.PIN_DUO_DUO.getCode() == platformId) {

        } else {

        }
        return resultList;
    }

    @Override
    public List<List<Object>> getExportItems(ItemQueryRequest itemQueryRequest) {
        List<List<Object>> resultList = new ArrayList<>();
        List<ItemVO> itemVOList = findItemList(itemQueryRequest);
        int platformId = itemQueryRequest.getPlatformId();
        int size = itemVOList.size();
        if (size > 0) {
            if (Platform.TAO_BAO.getCode() == platformId) {
                for (int i = 0; i < size; i++) {
                    ItemVO item = itemVOList.get(i);
                    List<Object> list = new ArrayList<>();
                    list.add(item.getId());
                    list.add(item.getItemName());
                    list.add(item.getShopName());
                    list.add(item.getPrice());
                    list.add(item.getSaleCount());
                    list.add(item.getCommentCount());
                    list.add(item.getLocation());
                    list.add(DateUtil.parseDate2String(item.getDatetime()));
                    list.add(item.getUrl());
                    resultList.add(list);
                }
            } else if (Platform.JING_DONG.getCode() == platformId) {
                for (int i = 0; i < size; i++) {
                    ItemVO item = itemVOList.get(i);
                    List<Object> list = new ArrayList<>();
                    list.add(item.getId());
                    list.add(item.getItemName());
                    list.add(item.getShopName());
                    list.add(item.getPrice());
                    list.add(item.getSaleCount());
//                    list.add(item.getCommentCount());
//                    list.add(item.getLocation());
                    list.add(DateUtil.parseDate2String(item.getDatetime()));
                    list.add(item.getUrl());
                    resultList.add(list);
                }
            } else if (Platform.PIN_DUO_DUO.getCode() == platformId) {

            } else {

            }
        }
        return resultList;
    }

    @Override
    public String[] getExcelHeaders(int platformId) {
        String[] result = null;
        switch (platformId) {
            case 1:
                result = new String[]{"序号", "商品名称", "店铺昵称", "价格", "付款数", "评论数", "地区", "日期", "商品链接"};
                break;
            case 2:
                result = new String[]{"序号", "商品名称", "店铺昵称", "价格", "付款数", "日期", "商品链接"};
                break;
            case 3:
                break;
            default:
                break;
        }
        return result;
    }

}
